Задача 2. Глобальная оптимизация


In [1]:
%matplotlib inline
import numpy as np
import scipy.optimize as optimize
import math

In [2]:
#заданная функция
def f(x):
    return np.sin(x/5.0) * np.exp(x/10.0) + 5 * np.exp (-x/2.0)
bounds = [(1.0,30.0)]
result = optimize.differential_evolution(f,bounds)

In [3]:
result


Out[3]:
     fun: array([-11.89889467])
     jac: array([ 0.])
 message: 'Optimization terminated successfully.'
    nfev: 83
     nit: 4
 success: True
       x: array([ 25.88019304])

In [4]:
file_obj = open('result-week3-Task2.txt', 'w')
file_obj.write(str(round(result.fun,2)))
file_obj.close()

In [5]:
# сравнил эффективность
x_val = 30
res = optimize.minimize(f,x_val,method="BFGS")

In [6]:
res


Out[6]:
      fun: -11.898894665981313
 hess_inv: array([[ 1.67936744]])
      jac: array([ 0.])
  message: 'Optimization terminated successfully.'
     nfev: 21
      nit: 6
     njev: 7
   status: 0
  success: True
        x: array([ 25.88019321])

In [ ]: